(function (vc) {

    vc.extends({
        propTypes: {
            callBackListener: vc.propTypes.string, //父组件名称
            callBackFunction: vc.propTypes.string //父组件监听方法
        },
        data: {
            addAffiliateRuleInfo: {
                ruleId: '',
                name: '',
                level1Rate: '',
                expireDay: '',

            }
        },
        _initMethod: function () {

        },
        _initEvent: function () {
            vc.on('addAffiliateRule', 'openAddAffiliateRuleModal', function () {
                $('#addAffiliateRuleModel').modal('show');
            });
        },
        methods: {
            addAffiliateRuleValidate() {
                return vc.validate.validate({
                    addAffiliateRuleInfo: vc.component.addAffiliateRuleInfo
                }, {
                    'addAffiliateRuleInfo.name': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "规则名称不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "30",
                            errInfo: "规则名称不能超过30"
                        },
                    ],
                    'addAffiliateRuleInfo.level1Rate': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "一级分销佣金比例不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "32",
                            errInfo: "一级分销佣金比例不能为空"
                        },
                    ],
                    'addAffiliateRuleInfo.level2Rate': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "二级分销佣金比例"
                        },
                    ],
                    'addAffiliateRuleInfo.level21Rate': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "二级时一级佣金比例"
                        },
                    ],
                    'addAffiliateRuleInfo.expireDay': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "过期时间不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "200",
                            errInfo: "过期时间不能为空"
                        },
                    ],


                });
            },
            saveAffiliateRuleInfo: function () {
                if (!vc.component.addAffiliateRuleValidate()) {
                    vc.toast(vc.validate.errInfo);

                    return;
                }

                vc.component.addAffiliateRuleInfo.communityId = vc.getCurrentCommunity().communityId;
                //不提交数据将数据 回调给侦听处理
                if (vc.notNull($props.callBackListener)) {
                    vc.emit($props.callBackListener, $props.callBackFunction, vc.component.addAffiliateRuleInfo);
                    $('#addAffiliateRuleModel').modal('hide');
                    return;
                }

                vc.http.apiPost(
                    'affiliateRule.saveAffiliateRule',
                    JSON.stringify(vc.component.addAffiliateRuleInfo),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            $('#addAffiliateRuleModel').modal('hide');
                            vc.component.clearAddAffiliateRuleInfo();
                            vc.emit('affiliateRuleManage', 'listAffiliateRule', {});

                            return;
                        }
                        vc.message(_json.msg);

                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');

                        vc.message(errInfo);

                    });
            },
            clearAddAffiliateRuleInfo: function () {
                vc.component.addAffiliateRuleInfo = {
                    name: '',
                    level1Rate: '',
                    expireDay: '',

                };
            }
        }
    });

})(window.vc);
